#!/bin/sh

set -o errexit
set -o nounset

SCRIPTS_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
ROOT_DIR="$(dirname -- "$SCRIPTS_DIR")"
DATA_DIR="$ROOT_DIR/data"

. "$SCRIPTS_DIR/common"

# Get the list of PHP extensions from the data file
#
# Arguments:
# $1: the path to the data file
#
# Output:
# the list of PHP extensions
getExtensionList() {
	getExtensionList_extensions=''
	IFS='
'
	for getExtensionList_line in $(cat -- "$1" | sort); do
		getExtensionList_extension=''
		IFS=' 	'
		for getExtensionList_chunk in $getExtensionList_line; do
			if test -z "$getExtensionList_extensions"; then
				getExtensionList_extensions="$getExtensionList_chunk"
			else
				getExtensionList_extensions="$getExtensionList_extensions $getExtensionList_chunk"
			fi
			break
		done
	done
	printf '%s' "$getExtensionList_extensions"
}

# Get the list of PHP versions from the data file
#
# Arguments:
# $1: the path to the data file
#
# Output:
# the list of PHP versions
getVersionList() {
	getVersionList_versions=''
	IFS='
'
	for getVersionList_line in $(cat -- "$1" | sort); do
		getVersionList_extension=''
		IFS=' 	'
		for getVersionList_chunk in $getVersionList_line; do
			if test -z "$getVersionList_extension"; then
				getVersionList_extension="$getVersionList_chunk"
			elif test -z "$getVersionList_versions"; then
				getVersionList_versions="$getVersionList_chunk"
			elif ! stringInList "$getVersionList_chunk" "$getVersionList_versions"; then
				getVersionList_versions="$getVersionList_versions $getVersionList_chunk"
			fi
		done
	done
	sortVersionList "$getVersionList_versions"
}

# Sort the list of PHP versions
#
# Arguments:
# $1: the list of PHP versions
#
# Output:
# the sorted list of PHP versions
sortVersionList() {
	sortVersionList_list=''
	IFS='
'
	for sortVersionList_version in $(printf '%s' "$1" | tr ' ' '\n' | sort -t '.' -k 1,1n -k 2,2n -k 3,3n); do
		if test -z "$sortVersionList_list"; then
			sortVersionList_list="$sortVersionList_version"
		else
			sortVersionList_list="$sortVersionList_version $sortVersionList_list"
		fi
	done
	printf '%s' "$sortVersionList_list"
}

# Generate the markdown table with the supported PHP extensions
#
# Arguments:
# $1: the path to the data file
# $2: the path to the special requirements data file
#
# Output:
# the markdown table
generateExtensionsTable() {
	generateExtensionsTable_data=$(cat "$1")
	generateExtensionsTable_extensions="$(getExtensionList "$1")"
	generateExtensionsTable_versions="$(getVersionList "$1")"
	generateExtensionsTable_count=0
	if test -f "$2"; then
		generateExtensionsTable_specialReqExt=$(cat "$2" | cut -d' ' -f1)
	else
		generateExtensionsTable_specialReqExt=''
	fi
	printf '| Extension |'
	IFS=' '
	for generateExtensionsTable_version in $generateExtensionsTable_versions; do
		printf ' PHP %s |' "$generateExtensionsTable_version"
	done
	printf '\n'
	printf '|:---:|'
	for generateExtensionsTable_version in $generateExtensionsTable_versions; do
		printf -- ':---:|'
	done
	printf '\n'
	IFS=' '
	for generateExtensionsTable_extension in $generateExtensionsTable_extensions; do
		generateExtensionsTable_count=$((generateExtensionsTable_count + 1))
		printf '| %s' "$generateExtensionsTable_extension"
		if stringInList "$generateExtensionsTable_extension" "$generateExtensionsTable_specialReqExt"; then
			printf '[*](#special-requirements-for-%s)' "$generateExtensionsTable_extension"
		fi
		printf ' |'
		IFS=' '
		for generateExtensionsTable_version in $generateExtensionsTable_versions; do
			printf ' '
			generateExtensionsTable_versionOk=''
			IFS='
'
			for generateExtensionsTable_dataLine in $generateExtensionsTable_data; do
				if stringInList "$generateExtensionsTable_extension" "$generateExtensionsTable_dataLine"; then
					if stringInList "$generateExtensionsTable_version" "$generateExtensionsTable_dataLine"; then
						generateExtensionsTable_versionOk='y'
					fi
					break
				fi
			done
			if test -n "$generateExtensionsTable_versionOk"; then
				printf '&check;'
			fi
			printf ' |'
		done
		printf '\n'
	done
	printf '\n'
	printf '*Number of supported extensions: %s*\n' $generateExtensionsTable_count
}

# Generate the markdown table with the special requirements for the PHP extensions
#
# Arguments:
# $1: the path to the data file
#
# Output:
# the markdown table
generateSpecialRequirementsTable() {
	generateSpecialRequirementsTable_started=0
	IFS='
'
	for generateSpecialRequirementsTable_line in $(cat -- "$1" | sort); do
		resetIFS
		generateSpecialRequirementsTable_index=0
		for generateSpecialRequirementsTable_chunk in $generateSpecialRequirementsTable_line; do
			if test $generateSpecialRequirementsTable_index -eq 0; then
				generateSpecialRequirementsTable_extension="$generateSpecialRequirementsTable_chunk"
			else
				if test $generateSpecialRequirementsTable_index -eq 1; then
					generateSpecialRequirementsTable_requirements=''
				elif test $generateSpecialRequirementsTable_index -eq 2; then
					generateSpecialRequirementsTable_requirements="$(printf -- '&bull; %s<br />&bull; ' "$generateSpecialRequirementsTable_requirements")"
				else
					generateSpecialRequirementsTable_requirements="$(printf '%s<br />&bull; ' "$generateSpecialRequirementsTable_requirements")"
				fi
				case "$generateSpecialRequirementsTable_chunk" in
					zts)
						generateSpecialRequirementsTable_chunk='Requires images with PHP compiled with thread-safety enabled (`zts`)'
						;;
					!arm)
						generateSpecialRequirementsTable_chunk='Not available in ARM architectures'
						;;
					!*)
						generateSpecialRequirementsTable_chunk="$(printf 'Not available in `%s` docker images' "${generateSpecialRequirementsTable_chunk#!}")"
						;;
				esac
				generateSpecialRequirementsTable_requirements="$(printf '%s%s' "$generateSpecialRequirementsTable_requirements" "$generateSpecialRequirementsTable_chunk")"
			fi
			generateSpecialRequirementsTable_index=$((generateSpecialRequirementsTable_index + 1))
		done
		if test $generateSpecialRequirementsTable_index -gt 1; then
			if test $generateSpecialRequirementsTable_started -eq 0; then
				printf '| Extension | Requirements |\n'
				printf '|---|---|\n'
				generateSpecialRequirementsTable_started=1
			fi
			printf '| <a name="special-requirements-for-%s"></a>%s | %s |\n' "$generateSpecialRequirementsTable_extension" "$generateSpecialRequirementsTable_extension" "$generateSpecialRequirementsTable_requirements"
		fi
	done
}

# Generate the contents of README.md
#
# Arguments:
# $1: the path to the README file
# $2: the path to the extensions data file
# $3: the path to the special requirements data file
#
# Output:
# the markdown table
generateReadme() {
	generateReadme_ph_ExtensionsStart='<!-- START OF EXTENSIONS TABLE -->'
	generateReadme_ph_ExtensionsEnd='<!-- END OF EXTENSIONS TABLE -->'
	generateReadme_ph_SpecialStart='<!-- START OF SPECIAL REQUIREMENTS -->'
	generateReadme_ph_SpecialEnd='<!-- END OF SPECIAL REQUIREMENTS -->'
	generateReadme_skip=''
	while IFS= read -r generateReadme_line; do
		if test "$generateReadme_line" = "$generateReadme_ph_ExtensionsEnd" || test "$generateReadme_line" = "$generateReadme_ph_SpecialEnd"; then
			generateReadme_skip=''
		fi
		if test -z "$generateReadme_skip"; then
			printf '%s\n' "$generateReadme_line"
		fi
		if test "$generateReadme_line" = "$generateReadme_ph_ExtensionsStart"; then
			generateReadme_skip='y'
			printf '<!-- ########################################################### -->\n'
			printf '<!-- #                                                         # -->\n'
			printf '<!-- #  DO NOT EDIT THIS TABLE: IT IS GENERATED AUTOMATICALLY  # -->\n'
			printf '<!-- #                                                         # -->\n'
			printf '<!-- #  EDIT THE data/supported-extensions FILE INSTEAD        # -->\n'
			printf '<!-- #                                                         # -->\n'
			printf '<!-- ########################################################### -->\n'
			generateExtensionsTable "$2" "$3"
		elif test "$generateReadme_line" = "$generateReadme_ph_SpecialStart"; then
			generateReadme_skip='y'
			printf '<!-- ########################################################### -->\n'
			printf '<!-- #                                                         # -->\n'
			printf '<!-- #  DO NOT EDIT THIS TABLE: IT IS GENERATED AUTOMATICALLY  # -->\n'
			printf '<!-- #                                                         # -->\n'
			printf '<!-- #  EDIT THE data/special-requirements FILE INSTEAD        # -->\n'
			printf '<!-- #                                                         # -->\n'
			printf '<!-- ########################################################### -->\n'
			generateSpecialRequirementsTable "$3"
		fi
	done <"$1"
	printf '%s' "$generateReadme_line"
}

NEW_README="$(generateReadme "$ROOT_DIR/README.md" "$DATA_DIR/supported-extensions" "$DATA_DIR/special-requirements")"
printf '%s\n' "$NEW_README" >"$ROOT_DIR/README.md"
